home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / Make / source / ar.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-27  |  7.8 KB  |  330 lines

  1. /* Interface to `ar' archives for GNU Make.
  2. Copyright (C) 1988,89,90,91,92,93,97 Free Software Foundation, Inc.
  3. This file is part of GNU Make.
  4.  
  5. GNU Make is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. GNU Make is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU Make; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "make.h"
  20.  
  21. #ifndef    NO_ARCHIVES
  22.  
  23. #include "filedef.h"
  24. #include "dep.h"
  25. #include <fnmatch.h>
  26.  
  27. /* Defined in arscan.c.  */
  28. extern long int ar_scan PARAMS ((char *archive, long int (*function) (), long int arg));
  29. extern int ar_name_equal PARAMS ((char *name, char *mem, int truncated));
  30. #ifndef VMS
  31. extern int ar_member_touch PARAMS ((char *arname, char *memname));
  32. #endif
  33.  
  34. /* Return nonzero if NAME is an archive-member reference, zero if not.
  35.    An archive-member reference is a name like `lib(member)'.
  36.    If a name like `lib((entry))' is used, a fatal error is signaled at
  37.    the attempt to use this unsupported feature.  */
  38.  
  39. int
  40. ar_name (name)
  41.      char *name;
  42. {
  43.   char *p = index (name, '('), *end = name + strlen (name) - 1;
  44.  
  45.   if (p == 0 || p == name || *end != ')')
  46.     return 0;
  47.  
  48.   if (p[1] == '(' && end[-1] == ')')
  49.     fatal ("attempt to use unsupported feature: `%s'", name);
  50.  
  51.   return 1;
  52. }
  53.  
  54.  
  55. /* Parse the archive-member reference NAME into the archive and member names.
  56.    Put the malloc'd archive name in *ARNAME_P if ARNAME_P is non-nil;
  57.    put the malloc'd member name in *MEMNAME_P if MEMNAME_P is non-nil.  */
  58.  
  59. void
  60. ar_parse_name (name, arname_p, memname_p)
  61.      char *name, **arname_p, **memname_p;
  62. {
  63.   char *p = index (name, '('), *end = name + strlen (name) - 1;
  64.  
  65.   if (arname_p != 0)
  66.     *arname_p = savestring (name, p - name);
  67.  
  68.   if (memname_p != 0)
  69.     *memname_p = savestring (p + 1, end - (p + 1));
  70. }
  71.  
  72. static long int ar_member_date_1 PARAMS ((int desc, char *mem, int truncated, long int hdrpos,
  73.     long int datapos, long int size, long int date, int uid, int gid, int mode, char *name));
  74.  
  75. /* Return the modtime of NAME.  */
  76.  
  77. time_t
  78. ar_member_date (name)
  79.      char *name;
  80. {
  81.   char *arname;
  82.   int arname_used = 0;
  83.   char *memname;
  84.   long int val;
  85.  
  86.   ar_parse_name (name, &arname, &memname);
  87.  
  88.   /* Make sure we know the modtime of the archive itself because we are
  89.      likely to be called just before commands to remake a member are run,
  90.      and they will change the archive itself.
  91.  
  92.      But we must be careful not to enter_file the archive itself if it does
  93.      not exist, because pattern_search assumes that files found in the data
  94.      base exist or can be made.  */
  95.   {
  96.     struct file *arfile;
  97.     arfile = lookup_file (arname);
  98.     if (arfile == 0 && file_exists_p (arname))
  99.       {
  100.     arfile = enter_file (arname);
  101.     arname_used = 1;
  102.       }
  103.  
  104.     if (arfile != 0)
  105.       (void) f_mtime (arfile, 0);
  106.   }
  107.  
  108.   val = ar_scan (arname, ar_member_date_1, (long int) memname);
  109.  
  110.   if (!arname_used)
  111.     free (arname);
  112.   free (memname);
  113.  
  114.   return (val <= 0 ? (time_t) -1 : (time_t) val);
  115. }
  116.  
  117. /* This function is called by `ar_scan' to find which member to look at.  */
  118.  
  119. /* ARGSUSED */
  120. static long int
  121. ar_member_date_1 (desc, mem, truncated,
  122.           hdrpos, datapos, size, date, uid, gid, mode, name)
  123.      int desc;
  124.      char *mem;
  125.      int truncated;
  126.      long int hdrpos, datapos, size, date;
  127.      int uid, gid, mode;
  128.      char *name;
  129. {
  130.   return ar_name_equal (name, mem, truncated) ? date : 0;
  131. }
  132.  
  133. /* Set the archive-member NAME's modtime to now.  */
  134.  
  135. #ifdef VMS
  136. int
  137. ar_touch (name)
  138.      char *name;
  139. {
  140.   error ("touch archive member is not available on VMS");
  141.   return -1;
  142. }
  143. #else
  144. int
  145. ar_touch (name)
  146.      char *name;
  147. {
  148.   char *arname, *memname;
  149.   int arname_used = 0;
  150.   register int val;
  151.  
  152.   ar_parse_name (name, &arname, &memname);
  153.  
  154.   /* Make sure we know the modtime of the archive itself before we
  155.      touch the member, since this will change the archive itself.  */
  156.   {
  157.     struct file *arfile;
  158.     arfile = lookup_file (arname);
  159.     if (arfile == 0)
  160.       {
  161.     arfile = enter_file (arname);
  162.     arname_used = 1;
  163.       }
  164.  
  165.     (void) f_mtime (arfile, 0);
  166.   }
  167.  
  168.   val = 1;
  169.   switch (ar_member_touch (arname, memname))
  170.     {
  171.     case -1:
  172.       error ("touch: Archive `%s' does not exist", arname);
  173.       break;
  174.     case -2:
  175.       error ("touch: `%s' is not a valid archive", arname);
  176.       break;
  177.     case -3:
  178.       perror_with_name ("touch: ", arname);
  179.       break;
  180.     case 1:
  181.       error ("touch: Member `%s' does not exist in `%s'", memname, arname);
  182.       break;
  183.     case 0:
  184.       val = 0;
  185.       break;
  186.     default:
  187.       error ("touch: Bad return code from ar_member_touch on `%s'", name);
  188.     }
  189.  
  190.   if (!arname_used)
  191.     free (arname);
  192.   free (memname);
  193.  
  194.   return val;
  195. }
  196. #endif /* !VMS */
  197.  
  198. /* State of an `ar_glob' run, passed to `ar_glob_match'.  */
  199.  
  200. struct ar_glob_state
  201.   {
  202.     char *arname;
  203.     char *pattern;
  204.     unsigned int size;
  205.     struct nameseq *chain;
  206.     unsigned int n;
  207.   };
  208.  
  209. /* This function is called by `ar_scan' to match one archive
  210.    element against the pattern in STATE.  */
  211.  
  212. static long int
  213. ar_glob_match (desc, mem, truncated,
  214.            hdrpos, datapos, size, date, uid, gid, mode,
  215.            state)
  216.      int desc;
  217.      char *mem;
  218.      int truncated;
  219.      long int hdrpos, datapos, size, date;
  220.      int uid, gid, mode;
  221.      struct ar_glob_state *state;
  222. {
  223.   if (fnmatch (state->pattern, mem, FNM_PATHNAME|FNM_PERIOD) == 0)
  224.     {
  225.       /* We have a match.  Add it to the chain.  */
  226.       struct nameseq *new = (struct nameseq *) xmalloc (state->size);
  227.       new->name = concat (state->arname, mem, ")");
  228.       new->next = state->chain;
  229.       state->chain = new;
  230.       ++state->n;
  231.     }
  232.  
  233.   return 0L;
  234. }
  235.  
  236. /* Alphabetic sorting function for `qsort'.  */
  237.  
  238. static int
  239. ar_glob_alphacompare (a, b)
  240.      char **a, **b;
  241. {
  242.   return strcmp (*a, *b);
  243. }
  244.  
  245. /* Return nonzero if PATTERN contains any metacharacters.
  246.    Metacharacters can be quoted with backslashes if QUOTE is nonzero.  */
  247. static int
  248. glob_pattern_p (pattern, quote)
  249.      const char *pattern;
  250.      const int quote;
  251. {
  252.   register const char *p;
  253.   int open = 0;
  254.  
  255.   for (p = pattern; *p != '\0'; ++p)
  256.     switch (*p)
  257.       {
  258.       case '?':
  259.       case '*':
  260.     return 1;
  261.  
  262.       case '\\':
  263.     if (quote)
  264.       ++p;
  265.     break;
  266.  
  267.       case '[':
  268.     open = 1;
  269.     break;
  270.  
  271.       case ']':
  272.     if (open)
  273.       return 1;
  274.     break;
  275.       }
  276.  
  277.   return 0;
  278. }
  279.  
  280. /* Glob for MEMBER_PATTERN in archive ARNAME.
  281.    Return a malloc'd chain of matching elements (or nil if none).  */
  282.  
  283. struct nameseq *
  284. ar_glob (arname, member_pattern, size)
  285.      char *arname, *member_pattern;
  286.      unsigned int size;
  287. {
  288.   struct ar_glob_state state;
  289.   char **names;
  290.   struct nameseq *n;
  291.   unsigned int i;
  292.  
  293.   if (! glob_pattern_p (member_pattern, 1))
  294.     return 0;
  295.  
  296.   /* Scan the archive for matches.
  297.      ar_glob_match will accumulate them in STATE.chain.  */
  298.   i = strlen (arname);
  299.   state.arname = (char *) alloca (i + 2);
  300.   bcopy (arname, state.arname, i);
  301.   state.arname[i] = '(';
  302.   state.arname[i + 1] = '\0';
  303.   state.pattern = member_pattern;
  304.   state.size = size;
  305.   state.chain = 0;
  306.   state.n = 0;
  307.   (void) ar_scan (arname, ar_glob_match, (long int) &state);
  308.  
  309.   if (state.chain == 0)
  310.     return 0;
  311.  
  312.   /* Now put the names into a vector for sorting.  */
  313.   names = (char **) alloca (state.n * sizeof (char *));
  314.   i = 0;
  315.   for (n = state.chain; n != 0; n = n->next)
  316.     names[i++] = n->name;
  317.  
  318.   /* Sort them alphabetically.  */
  319.   qsort ((char *) names, i, sizeof (*names), ar_glob_alphacompare);
  320.  
  321.   /* Put them back into the chain in the sorted order.  */
  322.   i = 0;
  323.   for (n = state.chain; n != 0; n = n->next)
  324.     n->name = names[i++];
  325.  
  326.   return state.chain;
  327. }
  328.  
  329. #endif    /* Not NO_ARCHIVES.  */
  330.